package com.ce.pms.report.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.omg.CORBA.DATA_CONVERSION;

import com.ce.pms.report.dao.IReportDao;
import com.ce.pms.report.queryvo.AlreadyChargeItemListQueryVO;
import com.ce.pms.report.vo.AlreadyChargeItemListReportVO;
import com.ce.report.entity.CEReportDataSet;
import com.ce.report.exception.CEReportException;
import com.ce.report.service.ICEReportDataSetService;
import com.ce.report.vo.ReportsQueryBaseVo;
import com.ce.util.BlankUtil;
import com.ce.util.DateUtil;

/**
 *
 * 功能描述：财务已收款分项明细表Service
 * <p>版权所有：中企动力
 * <p>未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author 黄彦军
 * Created on 2008-10-28
 * Edit author:
 * Edit on:
 * 修改原因:
 */
public class AlreadyChargeItemListService implements ICEReportDataSetService {

	private IReportDao reportDao;

	public CEReportDataSet getReportDataSet(ReportsQueryBaseVo arg0) throws CEReportException {
		AlreadyChargeItemListQueryVO queryVO = (AlreadyChargeItemListQueryVO) arg0;

		return new CEReportDataSet(getData(queryVO),getHeadInfo(queryVO));
	}

	/**
	 * 功能描述:获取数据  <br>
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<AlreadyChargeItemListReportVO> getData(AlreadyChargeItemListQueryVO queryVO){

		String precinct = queryVO.getPrecinct();
		String buildId= "";
		if (BlankUtil.isBlank( queryVO.getBuildingId())){
			buildId = "%";
		}else{
			buildId = queryVO.getBuildingId().toString();
		}
		int subentry = queryVO.getSubentry();
		String computeMonth = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyyMM");

		Object[] obj = new Object[] { subentry, subentry, subentry, subentry,
				subentry, subentry, subentry, subentry, subentry, computeMonth,computeMonth,
				buildId, precinct };

		List<Object[]> temp = reportDao.getDataList("alreadyChargeItemList", obj);
		List<AlreadyChargeItemListReportVO> list = arrayToVo(temp);

		return list;
	}

	/**
	 * 功能描述:财务已收款分项明细数组 转为 VO 数组   <br>
	 * @param temp
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private List<AlreadyChargeItemListReportVO> arrayToVo (List<Object[]> temp){
		List<AlreadyChargeItemListReportVO> list = new ArrayList<AlreadyChargeItemListReportVO>();
		if (BlankUtil.isBlank(temp)){
			return null;
		}
		for (Object[] obj: temp) {
			AlreadyChargeItemListReportVO reportVO = new AlreadyChargeItemListReportVO();
			reportVO.setCompanyName((String)obj[0]);	//物业公司名称
			reportVO.setHouseNumber((String)obj[1]);	//房号
			reportVO.setUserName((String)obj[2]);		//客户名称
			reportVO.setAccounts((String)obj[3]);		//银行帐号
			reportVO.setComputeMonth((String)obj[4]);	//计费月份
			reportVO.setPayDate((Date)obj[5]);			//收费日期
			reportVO.setReceiptNO((String)obj[6]);		//单据号码
			reportVO.setActualPay(setValue(obj[7]));	//实收金额
			reportVO.setActualThis(setValue(obj[8]));	//实收本月
			reportVO.setActualBefore(setValue(obj[9]));//实收往月
			reportVO.setLatefee(setValue(obj[10]));	//滞纳金
			reportVO.setOverheadExpenses(setValue(obj[11]));//管理费
			reportVO.setOnticUpkeep(setValue(obj[12]));	//本体维修基金
			reportVO.setElectricityPay(setValue(obj[13]));//电费
			reportVO.setWaterPay(setValue(obj[14]));	//水费
			reportVO.setSewagePay(setValue(obj[15]));	//污水费
			reportVO.setGarbagePay(setValue(obj[16]));//垃圾处理费
			reportVO.setUpkeepPay(setValue(obj[17]));	//维修费
			reportVO.setAdjustSum(setValue(obj[18]));	//调整费用合计
			reportVO.setDerateSum(setValue(obj[19]));	//减免费用合计

			list.add(reportVO);
		}
		return list;
	}


		//传入的值必须是 BigDecimal 类型的,否则返回空 如果为空或值为0 则返回空
		private BigDecimal setValue(Object obj){
			if (obj instanceof BigDecimal) {
				if(((BigDecimal)obj).intValue() == 0){
					return null;
				}
				return (BigDecimal)obj;
			}
			return null;
		}

	/**
	 * 功能描述:获取头信息  <br>
	 * @param queryVO
	 * @return
	 * Create author:黄彦军 <br>
	 * Create on:2008-11-14  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	private Map<String, Object> getHeadInfo(AlreadyChargeItemListQueryVO queryVO){
		Map<String, Object> map = new HashMap<String, Object>();
		String computeMonth = DateUtil.convertDateToStr(queryVO.getComputeMonth(), "yyyyMM");
		map.put("reportHeadStr", "计费月份:"+computeMonth);
		map.put("printDate", new Date());

		String subentry = "";
		if(queryVO.getSubentry() == 1){
			subentry="本月费用";
		}else if(queryVO.getSubentry() == 2){
			subentry="往月欠款";
		}else{
			subentry="本月费用 + 往月欠款";
		}
		map.put("subentry", subentry);
		return map;
	}

	public void setReportDao(IReportDao reportDao) {
		this.reportDao = reportDao;
	}


}
